package com.example.kkblog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.kkblog.controller.dto.ResponseDto;
import com.example.kkblog.domain.Dynamic;
import com.example.kkblog.domain.Star;
import com.example.kkblog.domain.Topic;
import com.example.kkblog.domain.dto.DynamicPreDto;
import com.example.kkblog.domain.dto.UserDto;
import com.example.kkblog.domain.query.StarQuery;
import com.example.kkblog.mapper.LikeMapper;
import com.example.kkblog.mapper.StarMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author xiaoke
 * @date 2024/5/30
 */
@Controller
@RequestMapping("/star")
public class StarController {
    @Autowired
    private StarMapper starMapper;

    @Autowired
    private LikeMapper likeMapper;

    @ResponseBody
    @GetMapping("/doStar")
    public ResponseDto doStar(@RequestParam("dynamicId") Integer dynamicId, HttpSession session) {
        UserDto loginUser = (UserDto) session.getAttribute("user");
        if (loginUser == null) {
            return ResponseDto.Fail("请登录后进行操作");
        }
        QueryWrapper<Star> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dynamic_id", dynamicId).eq("user_id", loginUser.getId());
        List<Star> stars = starMapper.selectList(queryWrapper);
        if (!stars.isEmpty()) {
            return ResponseDto.Fail("您已经收藏该帖子！");
        }
        Star star = new Star();
        star.setDynamicId(dynamicId);
        star.setUserId(loginUser.getId());
        starMapper.insert(star);
        // 查询收藏数量
        QueryWrapper<Star> numQuery = new QueryWrapper<>();
        numQuery.eq("dynamic_id", dynamicId);
        return ResponseDto.Success("操作成功！", starMapper.selectCount(numQuery));
    }

    @ResponseBody
    @GetMapping("/doCancelStar")
    public ResponseDto cancelStar(@RequestParam("dynamicId") Integer dynamicId, HttpSession session) {
        UserDto loginUser = (UserDto) session.getAttribute("user");
        if (loginUser == null) {
            return ResponseDto.Fail("请登录后进行操作");
        }
        QueryWrapper<Star> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dynamic_id", dynamicId).eq("user_id", loginUser.getId());
        int delete = starMapper.delete(queryWrapper);
        if (delete == 0) {
            return ResponseDto.Fail("您还未收藏该帖子！");
        }
        // 查询收藏数量
        QueryWrapper<Star> numQuery = new QueryWrapper<>();
        numQuery.eq("dynamic_id", dynamicId);
        return ResponseDto.Success("操作成功！", starMapper.selectCount(numQuery));
    }

    // 加载更多
    @ResponseBody
    @GetMapping("/more")
    public ResponseDto loadMore(@RequestParam("dynamicId") Integer dynamicId,
                                @RequestParam(value = "pageNum", required = false) Integer pageNum,
                                HttpSession session) {
        UserDto loginUser = (UserDto) session.getAttribute("user");
        QueryWrapper<Star> starQueryWrapper = new QueryWrapper<>();
        starQueryWrapper.eq("dynamic_id", dynamicId).eq("user_id", loginUser.getId());
        Star star = starMapper.selectOne(starQueryWrapper);
        List<DynamicPreDto> dynamicPreDtos = starMapper.selectMoreStarDynamics(new StarQuery(loginUser.getId(), star.getId(), 5));
        // 查询是否对用户的动态点赞
        dynamicPreDtos.forEach(item -> {
            item.setLiked(likeMapper.selectIfUserLike(loginUser.getId() + "_" + item.getId()) > 0);
        });
        return ResponseDto.Success("查询成功！", dynamicPreDtos);
    }
}
